﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace qnd_FileUpdatesViewer
{
    public partial class Form1 : Form
    {
        //Main variables
        public Timer timer;
        string safefilename = "";
        FileStream fs;
        StreamReader sr;

        public Form1()
        {
            InitializeComponent();
        }

        private void toolStripLabel1_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                richTextBox1.Text = System.IO.File.ReadAllText(fd.FileName);
                safefilename = fd.FileName;
                fs = File.Open(safefilename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                sr = new StreamReader(fs);
                settimerspeed();
                timer.Start();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer = new Timer();
            settimerspeed();
            timer.Tick += new EventHandler(timer_Tick);
        }




        void timer_Tick(object sender, EventArgs e)
        {

            fs = File.Open(safefilename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            sr = new StreamReader(fs);

            richTextBox1.Text = "";
           
            richTextBox1.AppendText(sr.ReadToEnd());
            richTextBox1.ScrollToCaret();
            
    
        }
        public void settimerspeed()
        {

            timer.Interval = Convert.ToInt32(toolStripTextBox1.Text) * 1000;
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            timer.Stop();
        }

        private void toolStripLabel2_Click(object sender, EventArgs e)
        {
            settimerspeed();
        }
    }
}
